package com.iraeta.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.User;
import com.iraeta.service.sys.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController(value = "KeHuAdminController")
@RequestMapping("sys/kehuadmin")
public class KeHuAdminController extends BaseController {
    @Autowired
    UserService userService;

    @RequestMapping("pagekehu")
    public String pageKeHu(@RequestBody JSONObject jo){
        String userName = jo.getString("userName");
        this.pageSet(jo);
        List<User> userList = userService.listKeHuByName(userName);
        PageInfo<User> userPageInfo = this.getPageInfo(userList);
        return this.success(userPageInfo);
    }

    @RequestMapping("addeditkehu")
    public String addEditKeHu(@RequestBody JSONObject paramJb){
        int userId = paramJb.getIntValue("id");
        User user = userId == 0 ? new User() :  userService.getUser(userId);
        this.mapJsonToObj(paramJb, user);
        user.setPhoto("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        if(userId == 0){
            userService.addKeHu(user);
        }else {
            userService.updateUser(user);
        }
        return this.success();
    }

    @RequestMapping("listkehu")
    public String listKeHu(){
        List<User> userList = userService.listKeHuByName(null);
        return this.success(userList);
    }
}
